// pages/add-receiveingAdress/add-receiveingAdress.js
const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import {
	addressList,
	cooperation
} from '../../utils/request/apiurl'


Page({
	data: {
		uid: '',
		adressList: {
			relationName: '', //收货人
			relationTel: '', //收货人电话
			cooperationNetwork: '', //合作网点
			cooperationTel: '', //联系电话
			networkAdress: '', //网点地址
			adressDetail: '', //详细地址
		},
		cooperationId: '', //网点id
		gId:'',//购物车id
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// console.log(options,'// goodList[0].url.push(goodList[0].firstPicture)')
		// debugger
		var userInfo = wx.getStorageSync('userInfo');
		this.setData({
			uid: userInfo.uid,
			gId:options.gId,
		})
		// if(options.relationName!=''&&options.relationName!=undefined&&options.relationName!=null){
		// 	console.log(options.relationName,'options.linkPhone')
		// 	this.setData({
		// 		'adressList.relationName': options.relationName,
		// 	})
		// }
		// if(options.linkPhone!=''&&options.linkPhone!=undefined&&options.linkPhone!=null){
		// 	this.setData({
		// 		'adressList.relationTel': options.linkPhone,
		// 	})
		// }
		// if(options.cooperId!=''&&options.cooperId!=undefined&&options.cooperId!=null){
		// 	this.setData({
		// 		id: options.cooperId,
		// 	})
		// }
		// if(options.gId){
		// 	this.setData({
		// 		gId:options.gId,
		// 	})
		// }
		// console.log(this.data.id,'this.data.idthis.data.id')
		if (this.data.cooperationId != '' && this.data.cooperationId != undefined && this.data.cooperationId != null) {
			this.cooperationList();
		}
		// this.cooperationList();
		// console.log(this.data.adressList)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// console.log(this.data.id,'this.data.idthis.data.id')
		if (this.data.cooperationId != '' && this.data.cooperationId != undefined && this.data.cooperationId != null) {
			this.cooperationList();
		}else{
      console.log(wx.getStorageSync('userInfo'),'11111111111111111')
      var userInfo = wx.getStorageSync('userInfo');
      if (userInfo.phone != null && userInfo.phone != '' && userInfo.phone != undefined) {
        this.setData({
          'adressList.relationTel': userInfo.phone,
        })
      }
    }
    
    
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	// onPullDownRefresh: function() {

	// },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	// onReachBottom: function() {

	// },

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	cooperationList() {
			var source = {
				id: this.data.cooperationId
			}
			api.post(cooperation.getCooperationById, source).then(res => {
				// console.log(res)
				this.setData({
					'adressList.cooperationNetwork': res.cooperationName,
					'adressList.cooperationTel': res.linkPhone,
					'adressList.networkAdress':res.cooperationAddress,	
				})
			}).catch(err => {
				// console.log(err)
				wx.showToast({
					title: err.message,
					icon: 'none'
				})
			})
	},
	//添加地址
	addNewAddress() {
		if (this.data.adressList.relationName == '') {
			wx.showToast({
				title: '收货人不能为空',
				icon: 'none'
			})
			return false
		}
		if (this.data.adressList.relationTel == '') {
			wx.showToast({
				title: '收货人电话不能为空',
				icon: 'none'
			})
			return false
		}
		if (this.data.adressList.cooperationNetwork == '') {
			wx.showToast({
				title: '合作网点不能为空',
				icon: 'none'
			})
			return false
		}
		var source = {
			uid: this.data.uid,
			name: this.data.adressList.relationName,
			phone: this.data.adressList.relationTel,
			cooperationId: this.data.cooperationId,
		}
		// console.log(source)
		api.post(addressList.add, source).then(res => {
			// console.log(res)
			if(res>0){
				// var pages = getCurrentPages(); // 获取页面栈
				// var currPage = pages[pages.length - 1]; // 当前页面
				// var prevPage = pages[pages.length - 2]; // 上一个页面
				// console.log(prevPage.data,'prevPage.data.addressInfor')
				// prevPage.setData({
				// 	adress:e.currentTarget.dataset.adressid
				// })
				wx.navigateBack({
				  delta:1
				})
				// wx.redirectTo({
				// 	url: '../receiveing-adress/receiveing-adress?gId='+this.data.gId
				// })
			}
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	},
	cooperationNetwork() {
		wx.navigateTo({
			url: '../cooperation-network/cooperation-network?gId='+this.data.gId+'&relationName='+this.data.adressList.relationName+"&linkPhone="+this.data.adressList.relationTel
		})
	},
	bindRelationName(e){
		this.setData({
			'adressList.relationName':e.detail.value
		})
	},
	bindRelationTel(e){
		this.setData({
			'adressList.relationTel':e.detail.value
		})
	}
})
